package com.example.demorsa.controller;

/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import com.example.demorsa.client.RestClient;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.List;

/**
 * @author liyuncong
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-09-29
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-09-29                      liyuncong          Created
 */
@RestController
@RequestMapping(path = "/api/v1/up")
public class TestController {

    @Autowired
    RestTemplateBuilder restTemplateBuilder;

    @Autowired
    private ObjectMapper objectMapper;


    @GetMapping("/test")
    private String test1() throws JsonProcessingException {
        final RestTemplate build = restTemplateBuilder
            .setConnectTimeout(Duration.ofSeconds(3))
            .build();


        final ResponseEntity<String> infos =
            build.getForEntity("http://localhost:9191/api/v1/ssh-keys", String.class);
        final String body = infos.getBody();
        final RestClient.KeysInfo keysInfo =
            objectMapper.readValue(body, RestClient.KeysInfo.class);

        System.out.println(keysInfo.getPayload().getPublicKey());
        System.out.println(keysInfo.getPayload().getPrivateKey());
        return infos.toString();
    }

    @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    private ResponseEntity<Boolean> upPorts(
        @RequestBody PortInfo info
    ) {
        final List<String> pid = info.getPid();
        System.out.println("-------------------------------------");
        pid.forEach(System.out::println);
        System.out.println("-------------------------------------");
        return ResponseEntity.ok(Boolean.TRUE);
    }
}

class PortInfo {
    private List<String> pid;

    public PortInfo() {
    }

    public List<String> getPid() {
        return pid;
    }

    public void setPid(List<String> pid) {
        this.pid = pid;
    }
}
